// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-02-24 11:20
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.leetCode.putFruites;

import java.util.HashMap;
import java.util.Map;

public class slideWindow {
    // 滑动窗口
    // 右窗口的移动和左窗口的收缩
    public int totalFruit(int[] fruits) {
        int L = 0, R = 0;
        int len = fruits.length;
        int max = 0;
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        while (R < len) {
            map.put(fruits[R], map.getOrDefault(fruits[R], 0) + 1);
            ++R;
            while (map.size() > 2) {
                map.put(fruits[L], map.get(fruits[L]) - 1);
                if (map.get(fruits[L]) <= 0) {
                    map.remove(fruits[L]);
                }
                ++L;
            }
            max = Math.max(max, R - L);
        }
        return max;
    }
}
